package com.youdian.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youdian.R;
import com.youdian.adapter.BusinessAdapter;
import com.youdian.entity.BaseEntity;
import com.youdian.entity.Business;
import com.youdian.http.BaseObserver;
import com.youdian.http.RetrofitFactory;
import com.youdian.http.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by moker on 2017/12/14.
 */

public class MainFragment extends Fragment implements XRecyclerView.LoadingListener{

    @BindView(R.id.recycler_view)
    XRecyclerView mRecyclerView;

    private Unbinder unbinder;

    private BusinessAdapter mBAdapter;
    private List<Business> mBData = new ArrayList<>();

    private Activity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mActivity = getActivity();
        //LinearLayoutManager是线性布局，setOrientation可以设置他的方向是横向还是纵向。
        LinearLayoutManager layoutManager=new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mBAdapter=new BusinessAdapter(mBData,mActivity);
        mRecyclerView.setAdapter(mBAdapter);
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setLoadingListener(this);
        loadData();

    }


    public void loadData(){
        RetrofitFactory.getInstance().getBusinessList()
                .compose(RxSchedulers.<BaseEntity<List<Business>>>compose())
                .subscribe(new BaseObserver<List<Business>>(mActivity) {
                    @Override
                    protected void onHandleSuccess(List<Business> businesses) {
                        Log.i("Food",businesses.size()+"");
                        mBData.clear();
                        mBData.addAll(businesses);
                        mBAdapter.notifyDataSetChanged();
                        mRecyclerView.refreshComplete();
                    }
                });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(unbinder!=null) unbinder.unbind();
    }

    @Override
    public void onRefresh() {
       loadData();
    }

    @Override
    public void onLoadMore() {

    }
}
